package com.ubigate.android.qmode.main;

import com.ubigate.android.qmode.R;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.VideoView;

public class NFCReadingActivity extends Activity implements OnClickListener {

	// TextView statusMessage;
	LinearLayout statusMessage;
	ImageView confirmButton;
	ImageView loadingImage;
	LinearLayout informationLayout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		overridePendingTransition(0, 0);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.nfc_reading_activity);
		initScreen();
	}

	private void initScreen() {
		// statusMessage = (TextView)findViewById(R.id.status_message);
		statusMessage = (LinearLayout) findViewById(R.id.status_message);
		confirmButton = (ImageView) findViewById(R.id.confirm_button);
		confirmButton.setOnClickListener(this);

		loadingImage = (ImageView) findViewById(R.id.loading_image);
		informationLayout = (LinearLayout) findViewById(R.id.information_layout);
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (getIntent() == null) {
			return;
		}

		Tag myTag = (Tag) getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);
		if (myTag != null) {
			statusMessage.setVisibility(View.GONE);
			loadingImage.setVisibility(View.VISIBLE);

			new Thread(new Runnable() {

				@Override
				public void run() {
					try {
						Thread.sleep(1000);
						runOnUiThread(new Runnable() {
							public void run() {
								if (loadingImage.getVisibility() == View.VISIBLE) {
									loadingImage.setVisibility(View.GONE);
									informationLayout
											.setVisibility(View.VISIBLE);
								}
							}
						});
					} catch (Exception e) {
					}
				}
			}).start();
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		overridePendingTransition(0, 0);
	}

	@Override
	public void onClick(View v) {
		statusMessage.setVisibility(View.GONE);
		loadingImage.setVisibility(View.VISIBLE);
		
		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					Thread.sleep(1000);
					runOnUiThread(new Runnable() {
						public void run() {
							if (loadingImage.getVisibility() == View.VISIBLE) {
								loadingImage.setVisibility(View.GONE);
								informationLayout.setVisibility(View.VISIBLE);
								Intent intent = new Intent(NFCReadingActivity.this,VideoPlayerActivity.class);
								startActivity(intent);
							}
						}
					});
				} catch (Exception e) {
				}
			}
		}).start();

	}
}
